﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace UIT_CV_Management
{
    public class MonThiTotNghiep
    {
        private String id;
        public String maMonThiTotNghiep;
        public String tenMonThiTotNghiep;
        private float ketQuaThi;

        // database
        Database myDatabase = Database.shareDatabase;

        public MonThiTotNghiep(String _tenMonThiTotNghiep, float _ketQuaThi)
        {
            ketQuaThi = _ketQuaThi;
            tenMonThiTotNghiep = _tenMonThiTotNghiep;

            id = Database.getID("MonThiTotNghiep", "ID");
            maMonThiTotNghiep = this.getMaMonThiTotNghiepFromDatabase();
        }
        public MonThiTotNghiep(String _tenMonThiTotNghiep, float _ketQuaThi, int index)
        {
            ketQuaThi = _ketQuaThi;
            tenMonThiTotNghiep = _tenMonThiTotNghiep;

            id = Database.getID("MonThiTotNghiep", "ID");
            this.IDaddIndex(index);

            maMonThiTotNghiep = this.getMaMonThiTotNghiepFromDatabase();
        }
        public MonThiTotNghiep(String _id)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM MonThiTotNghiep WHERE ID = '" + _id + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                // get info
                id = table.Rows[0]["ID"].ToString();
                maMonThiTotNghiep = table.Rows[0]["MaMonThiTotNghiep"].ToString();
                tenMonThiTotNghiep = table.Rows[0]["TenMonThiTotNghiep"].ToString();
                ketQuaThi = float.Parse(table.Rows[0]["KetQuaThi"].ToString());

            }
        }
        private String getMaMonThiTotNghiepFromDatabase()
        {
            String x;
            x = Database.getID("MonThiTotNghiep", "MaMonThiTotNghiep");
            return x;
        }
        private void IDaddIndex(int _index)
        {
            Database.formatID("MaMonThiTotNghiep", _index + 1);
        }
        public Boolean Save()
        {
            String sqlMonThiTotNghiep = "INSERT INTO MonThiTotNgiep VALUES('" 
                + id + "','"
                + maMonThiTotNghiep + "',N'" 
                + tenMonThiTotNghiep + "'," 
                + ketQuaThi + ")";
            QueryResult result = myDatabase.Query(sqlMonThiTotNghiep);

            if (result.ReturnCode == 409)
            {
                return false;
            }
            return true;
        }
        public Boolean Update()
        {
            bool isDupplicated = Database.isDupplicate("MonThiTotNghiep", "ID", id);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE MonThiTotNghiep SET TenMonThiTotNghiep = N'" + tenMonThiTotNghiep + "', KetQuaThi = " + ketQuaThi
                    + " WHERE ID = '" + id + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    return false;
            }
            return true;
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM MonThiTotNghiep WHERE ID = '" + id + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            return true;
        }
    }
}
